package com.jaychen.tliaswebmanagement.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jaychen.tliaswebmanagement.mapper.StuMapper;
import com.jaychen.tliaswebmanagement.pojo.PageResult;
import com.jaychen.tliaswebmanagement.pojo.StuQueryParam;
import com.jaychen.tliaswebmanagement.pojo.Student;
import com.jaychen.tliaswebmanagement.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StuServiceImpl implements StuService {
    @Autowired
    private StuMapper stuMapper;

    @Override
    public PageResult<Student> page(StuQueryParam stuQueryParam) {
        PageHelper.startPage(stuQueryParam.getPage(), stuQueryParam.getPageSize());
        Page<Student> page = stuMapper.page(stuQueryParam);
        return new PageResult<Student>(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        stuMapper.deleteByIds(ids);
    }

    @Override
    public void add(Student stu) {
        stu.setCreateTime(LocalDateTime.now());
        stu.setUpdateTime(LocalDateTime.now());
        stuMapper.add(stu);
    }

    @Override
    public Student getById(Integer id) {
        return stuMapper.getById(id);
    }

    @Override
    public void update(Student stu) {
        stu.setUpdateTime(LocalDateTime.now());
        stuMapper.update(stu);
    }

    @Override
    public void violate(Integer id, Integer score) {
        stuMapper.violate(id,score);
    }
}
